package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.OrderService;
import com.baichang.taoshunda.service.ReservationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 17/08/24.
 */
@Controller
@RequestMapping(ControllerConstants.ORDER)
public class OrderMCtronller extends BaseMobileController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ReservationOrderService reservationOrderService;

    /**
     * 订单优惠金额
     */
    @RequestMapping("/getDiscountAmount")
    public void getDiscountAmount(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer bussId = getInt(json, "bussId", 0);
                Double money = getDouble(json, "money", 0);
                return orderService.getDiscountAmount(userId, bussId, money);
            }
        });
    }

    /**
     * 提交订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/submintOrder")
    public void submintOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String goods = getString(json, "goods", "");
                int userId = getInt(json, "userId", 0);
                int bussId = getInt(json, "bussId", 0);
                String bussName = getString(json, "bussName", "");
                int payType = getInt(json, "payType", 1);
                String userNmae = getString(json, "userName", "");
                String goodsAddress = getString(json, "goodsAddress", "");
                String invoiceHead = getString(json, "invoiceHead", "");
                String invoiceNumber = getString(json, "invoiceNumber", "");
                String phone = getString(json, "phone", "");
                String remak = getString(json, "remak", "");
                double fee = getDouble(json, "fee", 0);
                //1 普通订单  2预约订单  3自提订单
                int orderType = getInt(json, "orderType", 1);
                double dispatching = getDouble(json, "dispatching", 0);
                double goodsMoney = getDouble(json, "goodsMoney", 0);
                double allMoney = getDouble(json, "allMoney", 0);
                double ulng = getDouble(json, "ulng", 0);
                double redPacketMoney = getDouble(json, "redPacketMoney", 0);//红包金额
                double returnMoney = getDouble(json, "returnMoney", 0);  //除去红包的支付金额
                int isActivity = getInt(json, "isActivity", 0);  //0  不参与活动   1  参与活动
                double subMoney = getDouble(json, "subMoney", 0);
                double ulat = getDouble(json, "ulat", 0);
                String deliveryTime = getString(json, "deliveryTime", "");
                return orderService.submintOrder(goods, userId, bussId, bussName, payType, userNmae, goodsAddress, phone, remak, fee, dispatching, goodsMoney, allMoney, ulng, ulat, deliveryTime, subMoney, invoiceHead, invoiceNumber, redPacketMoney, returnMoney, isActivity, orderType);
            }
        });
    }

    /**
     * 提交预约订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/reservationOrder")
    public void reservationOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String goods = getString(json, "goods", "");
                int userId = getInt(json, "userId", 0);
                int bussId = getInt(json, "bussId", 0);
                String bussName = getString(json, "bussName", "");
                String username = getString(json, "username", "");
                String goodsAddress = getString(json, "goodsAddress", "");
                String phone = getString(json, "phone", "");
                String remark = getString(json, "remark", "");
                double allMoney = getDouble(json, "allMoney", 0);
                double redPacketMoney = getDouble(json, "redPacketMoney", 0);
                double returnMoney = getDouble(json, "returnMoney", 0);
                double subMoney = getDouble(json, "subMoney", 0);
                int isActivity = getInt(json, "isActivity", 0);
                return orderService.reservationOrder(goods, userId, bussId, bussName, username, goodsAddress, phone, remark, allMoney, redPacketMoney, returnMoney, isActivity, subMoney);
            }
        });
    }

    /**
     * 订单列表查询
     */
    @RequestMapping("/getOrderList")
    public void getOrderList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer orderState = getInt(json, "orderState", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                return orderService.getOrderList(orderState, userId, nowPage, pageSize);
            }
        });
    }

    /**
     * 订单详情查询
     */
    @RequestMapping("/getOrderDetialsByOrderNumber")
    public void getOrderDetialsByOrderNumber(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");

                return orderService.getOrderDetialsByOrderNumber(orderNumber);
            }
        });
    }

    /**
     * 跳转评价订单页面（返回订单的全部商品信息）
     */
    @RequestMapping("/getOrderGoodsList")
    public void getOrderGoodsList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");

                return orderService.getOrderGoodsList(orderNumber);
            }
        });
    }

    /**
     * 取消订单
     */
    @RequestMapping("/cancelOrder")
    public void cancelOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");

                return orderService.cancelOrder(orderNumber);
            }
        });
    }

    /**
     * 订单支付
     */
    @RequestMapping("/payOrder")
    public void payOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                String remark = getString(json, "remark", "");
                int payMethod = getInt(json, "payMethod", 0);
                return orderService.payOrder(orderNumber, payMethod, remark);
            }
        });
    }

    /**
     * 用户确认消费订单
     */
    @RequestMapping("/userConsumeOrder")
    public void userConsumeOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                Integer id = getInteger(json, "id", 0);
                return reservationOrderService.userConsumeOrder(orderNumber, id);
            }
        });
    }

    /**
     * 订单列表查询
     */
    @RequestMapping("/getReservationOrderList")
    public void getReservationOrderList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer orderState = getInt(json, "orderState", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                return orderService.getReservationOrderList(orderState, userId, nowPage, pageSize);
            }
        });
    }

    /**
     * 订单详情查询
     */
    @RequestMapping("/getReservationOrderDetails")
    public void getReservationOrderDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
//                Integer orderId = getInt(json, "orderId", 0);
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.getReservationOrderDetails(orderNumber);
            }
        });
    }

    /**
     * 申请退款
     */
    @RequestMapping("/planRefundment")
    public void planRefundment(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                int userId = getInt(json, "userId", 0);
                String orderNumber = getString(json, "orderNumber", "");
                String reasonRefund = getString(json, "reasonRefund", "");
                String reasonImg = getString(json, "reasonImg", "");
                return orderService.planRefundment(userId, orderNumber, reasonImg, reasonRefund);
            }
        });
    }

    /**
     * 获取订单状态详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getOrderStateDetail")
    public void getOrderStateDetail(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.getOrderStateDetail(orderNumber);
            }
        });
    }

    @RequestMapping("/notyfyTest")
    public void notyfyTest(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.updateOrderStateByOrderNumber(orderNumber);
            }
        });
    }

    /**
     * 申请退货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/applyReturnGoods")
    public void applyReturnGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                String reasonRefund = getString(json, "reasonRefund", "");
                String reasonImg = getString(json, "reasonImg", "");
                int params = getInt(json, "params", 1);
                return orderService.applyReturnGoods(orderNumber, params, reasonImg, reasonRefund);
            }
        });
    }

    /**
     * 申请退货(需求修改之后的)
     *
     * @param request
     * @param response
     */
    @RequestMapping("/applyReturnGoodsByOrderDetailId")
    public void applyReturnGoodsByOrderDetailId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                int orderDetailId = getInt(json, "orderDetailId", 0);
                String reasonRefund = getString(json, "reasonRefund", "");
                String reasonImg = getString(json, "reasonImg", "");
                int params = getInt(json, "params", 1);
                return orderService.applyReturnGoodsByOrderDetailId(orderNumber, orderDetailId, params, reasonImg, reasonRefund);
            }
        });
    }

    /**
     * 申请换货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/applyBarter")
    public void applyBarter(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                String barterRefund = getString(json, "barterRefund", "");
                int orderDetailId = getInt(json, "orderDetailId", 0);
                String barterImg = getString(json, "barterImg", "");
                int params = getInt(json, "params", 1);
                return orderService.applyBarter(orderNumber, params, barterImg, barterRefund, orderDetailId);
            }
        });
    }

    /**
     * 用户端删除订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/removeOrderUserStatus")
    public void removeOrderUserStatus(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");

                return orderService.removeOrderUserStatus(orderNumber);
            }
        });
    }

    /**
     * 用户可用红包金额
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getUserRedPacket")
    public void getUserRedPacket(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                return orderService.getUserRedPacket(userId);
            }
        });
    }

    /**
     * 查看订单和互动的未读总数
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getTrendCountAndOrderCount")
    public void getTrendCountAndOrderCount(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer trendsId = getInt(json, "trendsId", 0);
                return orderService.getTrendCountAndOrderCount(userId, trendsId);
            }
        });
    }

    /**
     * 查看订单商品的退换货理由
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findReasonByDetailsId")
    public void findReasonByDetailsId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderDetailsId = getInt(json, "orderDetailId", 0);
                return orderService.findReasonByDetailsId(orderDetailsId);
            }
        });
    }

    /**
     * 用户自提确认取货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/sureGoodsSelfIntroduction")
    public void sureGoodsSelfIntroduction(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");

                return orderService.sureGoodsSelfIntroduction(orderNumber);
            }
        });
    }

}
